package com.kgc.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.api.goods.GoodsItemApi;
import com.kgc.bean.GoodsItem;

import com.kgc.domain.GoodsItemExample;
import com.kgc.mapper.GoodsItemMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class GoodsItemProvider implements GoodsItemApi {
    @Autowired
    private GoodsItemMapper goodsItemMapper;
    @Override
    public GoodsItem returnProductInformationAccordingToProductId(String goodsId) {
        return goodsItemMapper.selectByPrimaryKey(goodsId);
    }
    //2、判断该id的商品是否存在
    @Override
    public Boolean whetherTheProductExists(String id) {
        GoodsItemExample goodsItemExample=new GoodsItemExample();
        GoodsItemExample.Criteria criteria=goodsItemExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        if(goodsItemMapper.selectByExample(goodsItemExample).size()>0){
            return true;
        }
        return false;
    }


    @Override
    public Boolean updateGoodsStockByGoodsId(String goodsId, Integer goodsStock) {
        return goodsItemMapper.uptGoodsStock(goodsId,goodsStock) > 0;
    }

    @Override
    public List<GoodsItem> selectGoodsItemByParentId(Integer parentId, Integer pageNo, Integer pageSize) {
        int num = (pageNo -1)*pageSize;
        if(num < 1){
            num = 0;
        }
        // 创建查询条件
        GoodsItemExample goodsItemExample=new GoodsItemExample();
        goodsItemExample.setDistinct(false);


        GoodsItemExample.Criteria criteria=goodsItemExample.createCriteria();
        criteria.andCidParent0EqualTo(parentId);
        // 分页
        goodsItemExample.setOffset((long) num);
        goodsItemExample.setLimit(pageSize);

        return goodsItemMapper.selectByExample(goodsItemExample);
    }

    @Override
    public List<GoodsItem> selectGoodsItemByKeyWord(String keyword, Integer pageNo, Integer pageSize) {
        int num = (pageNo -1)*pageSize;
        if(num < 1){
            num = 0;
        }
        // 创建查询条件
        GoodsItemExample goodsItemExample=new GoodsItemExample();
        goodsItemExample.setDistinct(false);

        GoodsItemExample.Criteria criteria=goodsItemExample.createCriteria();
        criteria.andGoodsNameLike("%" + keyword + "%");

        // 分页
        goodsItemExample.setOffset((long) num);
        goodsItemExample.setLimit(pageSize);

        return goodsItemMapper.selectByExample(goodsItemExample);
    }

    @Override
    public GoodsItem selectGoodsByGoodsId(String goodsId) {

        return goodsItemMapper.selectByPrimaryKey(goodsId);
    }


}
